package com.eliansun.controller;

import com.eliansun.crawler.engine.Task;
import com.eliansun.service.mapper.SpiderCommodityMapper;
import com.eliansun.service.service.impl.SpiderJobServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liujin
 * @description 采集任务控制
 * @create 2019/7/3
 */
@Controller
@RequestMapping(value = "/spider")
public class SpiderTaskController {

    @Resource
    private Task task;

    @Resource
    private SpiderCommodityMapper spiderCommodityMapper;
    @Resource
    private SpiderJobServiceImpl spiderJobService;

    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        spiderCommodityMapper.getList();
        return "success";
    }

    @RequestMapping(value = "/addTask")
    @ResponseBody
    public Map<String, Object> getTask() {
        Map<String, Object> result = new HashMap<>();
        try {
            task.run();
            result.put("status", 200);
            result.put("msg", "init success");
        } catch (Exception e) {
            result.put("status", 500);
            result.put("msg", "init fail");
        }
        return result;
    }

}